/**
 * <p>部门选择字段控件</p>
 * xtype = cceafdeptmentfield

 * @module Ext-ux-cceaf-control
 * @class cceaf.control.DeptmentField
 * @extends Ext.form.TriggerField
 * @since 1.0
 */
Ext.define('cceaf.control.DeptmentField',{
	extend: 'Ext.form.field.Trigger', 
	requires: [
	    'cceaf.ControlDefaultConfig'
    ],
    alias: 'widget.cceafdeptmentfield',
    requires: [
   	    'cceaf.control.SelectDeptmentGrid'
   	],
    
	triggerClass : 'x-form-date-trigger',
	
	/**
	 * 选择部门后要填充的部门ID的name
	 *
     * @property nameOfDeptid 
     * @type String
     * @default null
     */
	nameOfDeptid: null,
	
	initComponent : function(){
		config = this;
		var defaultFieldWidth = cceaf.ControlDefaultConfig.DEFAULT_FIELD_WIDTH;
		config = Ext.applyIf(config, {
			width: defaultFieldWidth
		});
		
		cceaf.control.DeptmentField.superclass.initComponent.call(this);

        this.addEvents(
            'select'
        );
    },

    initEvents: function() {
    	cceaf.control.DeptmentField.superclass.initEvents.call(this);
        this.keyNav = new Ext.KeyNav(this.el, {
            "down": function(e) {
                this.onTriggerClick();
            },
            scope: this,
            forceKeyDown: true
        });
    },
	
    onTriggerClick : function(){
        if(this.disabled){
            return;
        }
        
        this.showSelectWin();
    },
    
    listeners:{
		'focus': function(){
			this.selectText();
		},
		'specialkey': function(obj,e){
			if(e.getKey() == 13){//按Enter
				this.showSelectWin();
			}
			if(e.getKey() == 8){//按Backspace
				this.clearSelectValue();
			}
		}
    },
    
    clearSelectValue: function(){
    	if(this.nameOfDeptid != null && this.nameOfDeptid != ""){
    		var deptmentFieldFather = this.up();
    		deptmentFieldFather.down('[name=' + this.nameOfDeptid + ']').setValue('');
		}
    },
    
    showSelectWin: function(){
		var selectDeptmentWin = Ext.getCmp("_selectDeptmentWin");
		if(selectDeptmentWin == null){
			selectDeptmentWin = new Ext.Window({
	    		id:'_selectDeptmentWin',
	        	title: '选择部门',
	        	modal: true,
	        	closeAction: 'hide',
	        	//x: this.getPosition()[0],
	        	//y: this.getPosition()[1],
	        	height: 420,
	        	width: 650,
	        	autoScroll: true,
	        	layout: 'fit',
	        	listeners:{
	        		'show': function(){
	        			this.down('[name=q_deptment_deptname]').focus(true,200);
	        		}
	        	},
	        	items: new cceaf.control.SelectDeptmentGrid({deptmentField: this, nameOfDeptid: this.nameOfDeptid})
			});
		} else{
			//selectDeptmentWin.setPosition(this.getPosition()[0],this.getPosition()[1]);
			//selectDeptmentWin.removeAll();
			//selectDeptmentWin.insert(0, new cceaf.control.SelectDeptmentGrid({idOfDeptid:this.idOfDeptid,idOfDeptname:this.id}) );
		}
		
		selectDeptmentWin.show();

		var selectDeptmentGrid = selectDeptmentWin.down('cceafselectdeptmentgrid');
		selectDeptmentGrid.deptmentField = this;
		selectDeptmentGrid.nameOfDeptid = this.nameOfDeptid;
    }
});